

***************************************************
*PREPARING MEASURES OF DEMOCRACY AMONG NEIGHBORS
***************************************************

*Preliminaries

************************************************************/
*IMPORT DATA, CLEAN, INTERPOLATE GAPS, AND CORRECT ID CCODE*/
************************************************************/

*YOU WILL NEED TO ENTER THE NAME OF YOUR WORKING DIRECTORY HERE
cd "C:\Users\treis\Dropbox\Dem Breakdown\CPS\New folder"


***************************************************************
*POLITY V DATA (http://www.systemicpeace.org/inscr/p5v2018.xls)
***************************************************************

import excel "http://www.systemicpeace.org/inscr/p5v2018.xls",  firstrow clear
xtset ccode year
replace ccode = 347 if country=="Kosovo"
*remove duplicate Yugoslavia 1991 entry
drop if cyear==3451991
*combine Yugoslavia pre 2003 with Serbia and Montenegro 2003-2006
replace ccode = 342 if country=="Yugoslavia"
replace ccode=342 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode=342 if country=="Serbia"&year>2006
drop if country=="Serbia"&year==2006
replace ccode = 341 if country=="Montenegro"
*remove duplicate Ethiopia 1993 entry
drop if cyear==5291993
replace ccode=530 if country=="Ethiopia"
*consolidate Pakistan entries*
replace ccode=770 if country=="Pakistan"
*combine WG with Prussia and Germany with ccode = 255
drop if cyear==2601945
drop if cyear==2601990
replace ccode=255 if ccode==260
*combine USSR with Russia
drop if ccode==365&year==1922
replace ccode=365 if ccode==364
*use only up to 2018, last year with more than one case
replace polity2=. if year>2018

*make consistent cyear
drop cyear
gen cyear = ccode*10000 + year

*drop missing values
replace democ = . if democ<-10
replace autoc = . if autoc <-10
replace xrreg = . if xrreg <-10
replace xrcomp = . if xrcomp <-10
replace xropen = . if xropen <-10
replace xconst = . if xconst <-10
replace parreg = . if parreg <-10
replace parcomp = . if parcomp <-10
replace exrec = . if exrec <-10
replace exconst = . if exconst <-10
replace polcomp = . if polcomp<-10
replace polity2=. if polity<-10

xtset ccode year
by ccode: ipolate polity2 year, gen(pol3)
replace pol3=. if polity2==.&polity~=-77&polity~=-88
replace polity2=pol3

*I interpolate linearly to replace Polity's code for transitional or interregnum years (-88 and -77).
*I leave years of foreign occupation (polity = -66) as missing. Polity codes the interregnum years (polity=-77) as 0.
*This means that, for instance, during Afghanistan's civil war after 1991 the country's regime shot up to 0 from -8, only to fall back down
*to -7 when the Taliban took over. My code is not perfect--the -66 years are included in the interpolations, but then replaced by missing--but is very close to right.

*make marker for start of data--i.e. independence or entry to the dataset
gen polyear = year
replace polyear=. if polity==.
bysort ccode: egen minyear=min(polyear)
gen indep=0
replace indep=1 if year>minyear-1

*Make rescaled polity2 variable*
gen pol2norm = .
replace pol2norm = (5*polity2+50)/100 if polity2~=.
xtset ccode year
label var pol2norm "Polity2 on 0-1 scale"

*Make binary democracy variable
gen pol2more5 = .
replace pol2more5=1 if polity2>=6&polity2~=.
replace pol2more5=0 if polity2<6
label var pol2more5 "Polity2 >=6"

*all cases in which Polity2 falls below 6 having been above 6  
xtset ccode year
gen polbkd=.
replace polbkd=1 if l.pol2more5==1&pol2more5==0
replace polbkd=0 if l.pol2more5==1&pol2more5==1
label var polbkd "Polity2 falls from 6-10 to below 6"

gen polfact = 0
replace polfact=1 if parcomp==3
replace polfact=. if parcomp==.
label var parcomp "Polity2 rates participation as factionalized: parcomp"
xtset ccode year
gen lpolfact=l.polfact
gen lpolity2=l.polity2

save "breakdown cases.dta", replace

keep country year ccode cyear polity2 lpolity2 pol2norm pol2more5 polbkd indep parcomp polfact lpolfact
sort cyear
sleep 600
save "breakdown cases.dta", replace
sleep 600

keep ccode country year cyear polity2 indep parcomp polfact lpolity2
sort cyear
save "polshort.dta", replace

use "breakdown cases.dta", clear
keep ccode year cyear 
sort cyear
save "pol210short.dta", replace

******************************************
*VDEM DATA 
******************************************
use "V-Dem-CY-Full+Others-v12.dta", clear
xtset country_id year
gen ccode=.
replace ccode = 100 if country_name=="Colombia"
replace ccode = 1001 if country_name=="Hong Kong"|country_name=="China: Hong Kong SAR"|country_name=="Hong Kong SAR"|country_name=="Hong Kong SAR, China"|country_name=="China, Hong Kong SAR"|country_name=="Hong Kong, China"
replace ccode = 1002 if country_name=="Puerto Rico"
replace ccode = 1005 if country_name=="Palestine"|country_name=="State of Palestine"|country_name=="Palestinian Territories"|country_name=="West Bank and Gaza Strip"|country_name=="West Bank and Gaza"|country_name=="Israeli-Occupied Territories and Palestinian Authority"|country_name=="Palestinian Authority"|country_name=="West Bank"|country_name=="Palestine, State of"
replace ccode = 101 if country_name=="Venezuela"|country_name=="Venezuela (Bolivarian Republic of)"|country_name=="Venezuela, RB"|country_name=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country_name=="Guyana"
replace ccode = 115 if country_name=="Suriname"
replace ccode = 130 if country_name=="Ecuador"
replace ccode = 135 if country_name=="Peru"
replace ccode = 140 if country_name=="Brazil"
replace ccode = 145 if country_name=="Bolivia"|country_name=="Bolivia (Plurinational State of)"|country_name=="Bolivia, Plurinational State of"
replace ccode = 150 if country_name=="Paraguay"
replace ccode = 155 if country_name=="Chile"
replace ccode = 160 if country_name=="Argentina"
replace ccode = 165 if country_name=="Uruguay"
replace ccode = 166 if country_name=="Faeroe Islands"
replace ccode = 2 if country_name=="United States of America"|country_name=="United States"|country_name=="USA"|country_name=="US"
replace ccode = 20 if country_name=="Canada"
replace ccode = 200 if country_name=="Great Britain"|country_name=="United Kingdom"|country_name=="UK"
replace ccode = 201 if country_name=="Channel Islands"
replace ccode = 202 if country_name=="Isle of Man"
replace ccode = 205 if country_name=="Ireland"
replace ccode = 210 if country_name=="Netherlands"
replace ccode = 211 if country_name=="Belgium"
replace ccode = 212 if country_name=="Luxembourg"
replace ccode = 220 if country_name=="France"
replace ccode = 221 if country_name=="Monaco"
replace ccode = 223 if country_name=="Liechtenstein"
replace ccode = 225 if country_name=="Switzerland"
replace ccode = 230 if country_name=="Spain"
replace ccode = 232 if country_name=="Andorra"
replace ccode = 235 if country_name=="Portugal"
replace ccode = 255 if country_name=="FRG/Germany"|country_name=="West Germany"|country_name=="Germany, West"|country_name=="Germany"|country_name=="West Germany (FRG)"|country_name=="German Federal Republic"|country_name=="Germany West"
replace ccode = 265 if country_name=="GDR"|country_name=="East Germany"|country_name=="Germany, East"|country_name=="East Germany (GDR)"|country_name=="Germany East"|country_name=="German Democratic Republic"
replace ccode = 290 if country_name=="Poland"
replace ccode = 305 if country_name=="Austria"
replace ccode = 31 if country_name=="Bahamas"
replace ccode = 31 if country_name=="Bahamas"|country_name=="Bahamas, The"|country_name=="The Bahamas"
replace ccode = 310 if country_name=="Hungary"
replace ccode = 316 if country_name=="Czech Rep."|country_name=="Czech Republic"|country_name=="Czechia"
replace ccode = 316 if country_name=="Czechoslovakia"
replace ccode = 317 if country_name=="Slovak Republic"|country_name=="Slovakia"|country_name=="Slovak Rep."
replace ccode = 32 if country_name=="Aruba"
replace ccode = 325 if country_name=="Italy"
replace ccode = 33 if country_name=="Bermuda"
replace ccode = 331 if country_name=="San Marino"
replace ccode = 338 if country_name=="Malta"
replace ccode = 339 if country_name=="Albania"
replace ccode = 341 if country_name=="Montenegro"
replace ccode = 342 if country_name=="Serbia and Montenegro"|country_name=="Serbia-Montenegro"
replace ccode = 342 if country_name=="Serbia"
replace ccode = 342 if country_name=="Yugoslavia"
replace ccode = 343 if country_name=="F.Y.R. Macedonia"|country_name=="Macedonia, FYR"|country_name=="FYR Macedonia"|country_name=="North Macedonia"|country_name=="Macedonia"|country_name=="The former Yugoslav Republic of Macedonia"|country_name=="Macedonia, the former Yugoslav Republic of"|country_name=="TFYR Macedonia"|country_name=="Macedonia (FYROM)"
replace ccode = 344 if country_name=="Croatia"
replace ccode = 346 if country_name=="Bosnia and Herzegovina"|country_name=="Bosnia"|country_name=="Bosnia-Herz"|country_name=="Bosnia-Herzegovina"|country_name=="Bosnia & Herzegovina"
replace ccode = 347 if country_name=="Kosovo"
replace ccode = 349 if country_name=="Slovenia"
replace ccode = 350 if country_name=="Greece"
replace ccode = 352 if country_name=="Cyprus"|country_name=="Cyprus (Greek)"
replace ccode = 355 if country_name=="Bulgaria"
replace ccode = 359 if country_name=="Moldova"|country_name=="Republic of Moldova"|country_name=="Moldova, Republic of"
replace ccode = 360 if country_name=="Romania"
replace ccode = 365 if country_name=="Russia"|country_name=="Russian Federation"|country_name=="Russia/USSR"|country_name=="USSR"|country_name=="Soviet Union"
replace ccode = 366 if country_name=="Estonia"
replace ccode = 367 if country_name=="Latvia"
replace ccode = 368 if country_name=="Lithuania"
replace ccode = 369 if country_name=="Ukraine"
replace ccode = 370 if country_name=="Belarus"
replace ccode = 371 if country_name=="Armenia"
replace ccode = 372 if country_name=="Georgia"
replace ccode = 373 if country_name=="Azerbaijan"
replace ccode = 375 if country_name=="Finland"
replace ccode = 380 if country_name=="Sweden"
replace ccode = 385 if country_name=="Norway"
replace ccode = 390 if country_name=="Denmark"
replace ccode = 395 if country_name=="Iceland"
replace ccode = 396 if country_name=="Greenland"
replace ccode = 40 if country_name=="Cuba"
replace ccode = 402 if country_name=="C. Verde Is."|country_name=="Cabo Verde"|country_name=="Cape Verde"
replace ccode = 403 if country_name=="Sao Tome and Principe"|country_name=="São Tomé and Príncipe"|country_name=="Sao Tome"|country_name=="S. Tomé & Principe"
replace ccode = 404 if country_name=="Guinea-Bissau"|country_name=="Guinea Bissau"
replace ccode = 41 if country_name=="Haiti"
replace ccode = 411 if country_name=="Eq. Guinea"|country_name=="Equatorial Guinea"
replace ccode = 42 if country_name=="Dom. Rep."|country_name=="Dominican Rep."|country_name=="Dominican Republic"|country_name=="Dominican Rep"
replace ccode = 420 if country_name=="Gambia"|country_name=="Gambia, The"|country_name=="The Gambia"
replace ccode = 432 if country_name=="Mali"
replace ccode = 433 if country_name=="Senegal"
replace ccode = 434 if country_name=="Benin"
replace ccode = 435 if country_name=="Mauritania"
replace ccode = 436 if country_name=="Niger"
replace ccode = 437 if country_name=="C?te d'Ivoire"|country_name=="Cote d`Ivoire"|country_name=="Côte d´Ivoire"|country_name=="Côte d’Ivoire"|country_name=="Côte d'Ivoire"|country_name=="Cote d'Ivoire"|country_name=="Ivory Coast (Cote d'Ivoire)"|country_name=="Ivory Coast"|country_name=="Côte d'Ivoire"|country_name=="Côte D'Ivoire"|country_name=="Cote D'Ivoire"
replace ccode = 438 if country_name=="Guinea"
replace ccode = 439 if country_name=="Burkina Faso"
replace ccode = 450 if country_name=="Liberia"
replace ccode = 451 if country_name=="Sierra Leone"
replace ccode = 452 if country_name=="Ghana"
replace ccode = 461 if country_name=="Togo"
replace ccode = 471 if country_name=="Cameroon"
replace ccode = 475 if country_name=="Nigeria"
replace ccode = 481 if country_name=="Gabon"
replace ccode = 482 if country_name=="Cent. Af. Rep."
replace ccode = 482 if country_name=="Central Afr. Rep."|country_name=="Cen African Rep"|country_name=="Central African Rep."
replace ccode = 482 if country_name=="Central African Republic"
replace ccode = 483 if country_name=="Chad"
replace ccode = 484 if country_name=="Congo (Brazzaville)"
replace ccode = 484 if country_name=="Congo Brazzaville"|country_name=="Congo-Brz"
replace ccode = 484 if country_name=="Congo Republic"
replace ccode = 484 if country_name=="Congo"
replace ccode = 484 if country_name=="Congo, Rep. Of"
replace ccode = 484 if country_name=="Congo, Rep."|country_name=="Congo-Brazzaville"|country_name=="Republic of Congo"|country_name=="Congo, Republic"|country_name=="Republic of the Congo"
replace ccode = 484 if country_name=="Congo, Republic of"|country_name=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country_name=="Congo, Republicof"|country_name=="Congo (Rep.)"
replace ccode = 490 if country_name=="Congo (Democratic Republic of the)"|country_name=="Congo, the Democratic Republic of the"|country_name=="Congo (Dem. Rep.)"|country_name==" Congo, D.R."|country_name=="Congo Kinshasa"
replace ccode = 490 if country_name=="Congo (DRC)"|country_name=="Congo, Democratic Republic of (Kinshasa)"|country_name=="Zaire (Democ Republic Congo)"|country_name=="Congo/Zaire"
replace ccode = 490 if country_name=="Congo (Kinshasa)"|country_name=="Congo-Kinshasa"
replace ccode = 490 if country_name=="Congo, Dem. R."
replace ccode = 490 if country_name=="Congo, Dem. Rep."|country_name=="Congo, Democratic Republic"|country_name=="Democratic Republic of Congo"|country_name=="Zaire"
replace ccode = 490 if country_name=="Congo, Democratic Republic of"|country_name=="Congo, Dem. Rep."|country_name=="D.R. of the Congo"
replace ccode = 490 if country_name=="Democratic Republic of the Congo"
replace ccode = 500 if country_name=="Uganda"
replace ccode = 501 if country_name=="Kenya"
replace ccode = 51 if country_name=="Jamaica"
replace ccode = 510 if country_name=="Tanzania (United Republic of)"|country_name=="U.R. of Tanzania: Mainland"|country_name=="Tanzania, United Republic of"
replace ccode = 510 if country_name=="Tanzania"|country_name=="United Republic of Tanzania"
replace ccode = 516 if country_name=="Burundi"
replace ccode = 517 if country_name=="Rwanda"
replace ccode = 52 if country_name=="Trinidad & Tob."
replace ccode = 52 if country_name=="Trinidad & Tobago"
replace ccode = 52 if country_name=="Trinidad and Tobago"
replace ccode = 52 if country_name=="Trinidad-Tobago"
replace ccode = 520 if country_name=="Somalia"
replace ccode = 522 if country_name=="Djibouti"
replace ccode = 53 if country_name=="Barbados"
replace ccode = 530 if country_name=="Ethiopia"
replace ccode = 531 if country_name=="Eritrea"
replace ccode = 54 if country_name=="Dominica"
replace ccode = 540 if country_name=="Angola"
replace ccode = 541 if country_name=="Mozambique"
replace ccode = 55 if country_name=="Grenada"
replace ccode = 551 if country_name=="Zambia"
replace ccode = 552 if country_name=="Zimbabwe"|country_name=="Rhodesia"
replace ccode = 553 if country_name=="Malawi"
replace ccode = 56 if country_name=="Saint Lucia"
replace ccode = 56 if country_name=="St. Lucia"
replace ccode = 560 if country_name=="S. Africa"
replace ccode = 560 if country_name=="South Africa"
replace ccode = 565 if country_name=="Namibia"
replace ccode = 57 if country_name=="Saint Vincent and the Grenadines"|country_name=="St Vincent"
replace ccode = 57 if country_name=="St. Vincent & Grenadines"
replace ccode = 57 if country_name=="St. Vincent and Grenadines"
replace ccode = 57 if country_name=="St. Vincent and the Grenadines"
replace ccode = 570 if country_name=="Lesotho"
replace ccode = 571 if country_name=="Botswana"
replace ccode = 572 if country_name=="Swaziland"|country_name=="Eswatini"
replace ccode = 58 if country_name=="Antigua and Barbuda"|country_name=="Antigua & Barbuda"
replace ccode = 580 if country_name=="Madagascar"
replace ccode = 581 if country_name=="Comoro Is."
replace ccode = 581 if country_name=="Comoros"
replace ccode = 59 if country_name=="Cayman Islands"
replace ccode = 590 if country_name=="Mauritius"
replace ccode = 591 if country_name=="Seychelles"
replace ccode = 60 if country_name=="Saint Kitts & Nevis"|country_name=="Saint Kitts and Nevis"|country_name=="St Kitts"
replace ccode = 60 if country_name=="St. Kitts and Nevis"
replace ccode = 60 if country_name=="St.Kitts & Nevis"
replace ccode = 600 if country_name=="Morocco"
replace ccode = 61 if country_name=="Curacao"
replace ccode = 615 if country_name=="Algeria"
replace ccode = 616 if country_name=="Tunisia"
replace ccode = 62 if country_name=="Turks and Caicos Islands"
replace ccode = 620 if country_name=="Libya"
replace ccode = 625 if country_name=="Sudan"|country_name=="Sudan (Former)"
replace ccode = 626 if country_name=="South Sudan"
replace ccode = 63 if country_name=="Virgin Islands (U.S.)"
replace ccode = 630 if country_name=="Iran (Islamic Republic of)"|country_name=="Iran, Islamic Republic of"
replace ccode = 630 if country_name=="Iran"|country_name=="Iran (I.R.)"
replace ccode = 630 if country_name=="Iran"
replace ccode = 630 if country_name=="Iran, Islamic Rep."|country_name=="Islamic Republic of Iran"
replace ccode = 640 if country_name=="Turkey"
replace ccode = 645 if country_name=="Iraq"
replace ccode = 651 if country_name=="Egypt"
replace ccode = 651 if country_name=="Egypt"
replace ccode = 651 if country_name=="Egypt, Arab Rep."
replace ccode = 652 if country_name=="Syria"
replace ccode = 652 if country_name=="Syria"
replace ccode = 652 if country_name=="Syrian Arab Republic"
replace ccode = 660 if country_name=="Lebanon"
replace ccode = 663 if country_name=="Jordan"
replace ccode = 666 if country_name=="Israel"|country_name=="Israel and Occupied Territories**"|country_name=="Israel in pre-1967 borders"
replace ccode = 670 if country_name=="Saudi Arabia"
replace ccode = 678 if country_name=="Yemen Arab Republic"|country_name=="Yemen (A.R.)"|country_name=="North Yemen"|country_name=="Yemen (AR)"|country_name=="Yemen North"
replace ccode = 679 if country_name=="Yemen, Rep."|country_name=="Yemen"|country_name=="Yemen, Republic of"
replace ccode = 680 if country_name=="Yemen (PDR)"|country_name=="South Yemen"|country_name=="Yemen, South"|country_name=="SouthYemen"|country_name=="Yemen People's Republic"|country_name=="Yemen  (PDR)"|country_name=="Yemen South"
replace ccode = 690 if country_name=="Kuwait"
replace ccode = 692 if country_name=="Bahrain"
replace ccode = 694 if country_name=="Qatar"
replace ccode = 696 if country_name=="Unit. Arab Em."
replace ccode = 696 if country_name=="United Arab Emirates"|country_name=="UAE"|country_name=="United Arab Emirates#"
replace ccode = 698 if country_name=="Oman"
replace ccode = 70 if country_name=="Mexico"
replace ccode = 700 if country_name=="Afghanistan"
replace ccode = 701 if country_name=="Turkmenistan"
replace ccode = 702 if country_name=="Tajikistan"
replace ccode = 703 if country_name=="Kyrgyz Republic"
replace ccode = 703 if country_name=="Kyrgyzstan"
replace ccode = 703 if country_name=="Kyrgyzstan"
replace ccode = 704 if country_name=="Uzbekistan"
replace ccode = 705 if country_name=="Kazakhstan"
replace ccode = 710 if country_name=="China (PRC)"
replace ccode = 710 if country_name=="China"
replace ccode = 710 if country_name=="China, P. R."
replace ccode = 710 if country_name=="PRC"
replace ccode = 711 if country_name=="China: Macao SAR"|country_name=="Macao, China"
replace ccode = 711 if country_name=="Macao SAR, China"|country_name=="China, Macao SAR"
replace ccode = 711 if country_name=="Macao"|country_name=="Macau"
replace ccode = 712 if country_name=="Mongolia"
replace ccode = 713 if country_name=="Taiwan (ROC)"|country_name=="Taiwan Province of China"|country_name=="Taiwan, China"|country_name=="Taiwan, Province of China"
replace ccode = 713 if country_name=="Taiwan"
replace ccode = 713 if country_name=="Taiwan"
replace ccode = 731 if country_name=="Korea (North)"|country_name=="Korea North"|country_name=="Korea, Democratic People's Republic of"
replace ccode = 731 if country_name=="Korea, Dem. Rep."|country_name=="Democratic People's Republic of Korea"|country_name=="D.P.R. Korea"
replace ccode = 731 if country_name=="North Korea"|country_name=="Korea, North"
replace ccode = 731 if country_name=="PRK"|country_name=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country_name=="Korea (South)"|country_name=="Korea South"
replace ccode = 732 if country_name=="Korea, Rep."|country_name=="Korea"|country_name=="Korea (Rep.)"
replace ccode = 732 if country_name=="Korea, Republic of"
replace ccode = 732 if country_name=="Korea, South"
replace ccode = 732 if country_name=="ROK"
replace ccode = 732 if country_name=="South Korea"|country_name=="Korea, South"|country_name=="Republic of Korea"|country_name=="South Korea (Republic of Korea)"
replace ccode = 740 if country_name=="Japan"
replace ccode = 750 if country_name=="India"
replace ccode = 760 if country_name=="Bhutan"
replace ccode = 770 if country_name=="Pakistan"
replace ccode = 771 if country_name=="Bangladesh"
replace ccode = 775 if country_name=="Burma (Myanmar)"
replace ccode = 775 if country_name=="Burma"
replace ccode = 775 if country_name=="Myanmar"|country_name=="Myanmar (Burma)"
replace ccode = 780 if country_name=="Sri Lanka"
replace ccode = 781 if country_name=="Maldives"
replace ccode = 790 if country_name=="Nepal"
replace ccode = 80 if country_name=="Belize"
replace ccode = 800 if country_name=="Thailand"
replace ccode = 811 if country_name=="Cambodia"
replace ccode = 812 if country_name=="Lao PDR"|country_name=="Lao P.D.R."
replace ccode = 812 if country_name=="Lao People's Democratic Republic"
replace ccode = 812 if country_name=="Lao"|country_name=="Lao People's DR"
replace ccode = 812 if country_name=="Laos"
replace ccode = 812 if country_name=="Laos"
replace ccode = 817 if country_name=="Vietnam, South"|country_name=="South Vietnam"|country_name=="Republic of Vietnam"
replace ccode = 818 if country_name=="Viet Nam"|country_name=="Vietnam, Socialist Republic of"
replace ccode = 818 if country_name=="Vietnam"|country_name=="Vietnam North"
replace ccode = 820 if country_name=="Malaysia"
replace ccode = 830 if country_name=="Singapore"
replace ccode = 835 if country_name=="Brunei Darussalam"
replace ccode = 835 if country_name=="Brunei"
replace ccode = 835 if country_name=="Brunei"
replace ccode = 840 if country_name=="Philippines"|country_name=="Philippines +"
replace ccode = 850 if country_name=="Indonesia"
replace ccode = 860 if country_name=="East Timor"|country_name=="East Timor (Timor L'este)"
replace ccode = 860 if country_name=="Timor-Leste"|country_name=="Timor Leste"
replace ccode = 90 if country_name=="Guatemala"
replace ccode = 900 if country_name=="Australia"
replace ccode = 91 if country_name=="Honduras"
replace ccode = 910 if country_name=="P.N. Guinea"
replace ccode = 910 if country_name=="Papua New Guinea"|country_name=="P.N. Guinea"|country_name=="P. N. Guinea"
replace ccode = 92 if country_name=="El Salvador"
replace ccode = 920 if country_name=="New Zealand"
replace ccode = 93 if country_name=="Nicaragua"
replace ccode = 935 if country_name=="Vanuatu"
replace ccode = 94 if country_name=="Costa Rica"
replace ccode = 940 if country_name=="Solomon Is."
replace ccode = 940 if country_name=="Solomon Islands"
replace ccode = 941 if country_name=="Northern Mariana Islands"
replace ccode = 946 if country_name=="Kiribati"
replace ccode = 947 if country_name=="Tuvalu"
replace ccode = 95 if country_name=="Panama"
replace ccode = 950 if country_name=="Fiji"
replace ccode = 951 if country_name=="French Polynesia"
replace ccode = 952 if country_name=="New Caledonia"
replace ccode = 955 if country_name=="Tonga"
replace ccode = 983 if country_name=="Marshall Islands"
replace ccode = 986 if country_name=="Palau"
replace ccode = 987 if country_name=="Micronesia (Federated States of)"|country_name=="Micronesia (FS of)"|country_name=="Micronesia, Federated States of"
replace ccode = 987 if country_name=="Micronesia"
replace ccode = 987 if country_name=="Micronesia, Fed. Sts."
replace ccode = 990 if country_name=="Samoa"|country_name=="W. Samoa"|country_name=="Western Samoa"
replace ccode = 991 if country_name=="Guam"
replace ccode = 992 if country_name=="Nauru"
replace ccode = 993 if country_name=="Niue"
replace ccode = 994 if country_name=="Cook Islands"
replace ccode = 995 if country_name=="American Samoa"
replace ccode=35 if country_name=="Anguilla"
replace ccode=36 if country_name=="Curaçao"
replace ccode=37 if country_name=="Montserrat"
replace ccode=38 if country_name=="Sint Maarten (Dutch part)"
replace ccode=39 if country_name=="British Virgin Islands"
replace ccode=521 if country_name=="Somaliland"|country_name=="Somaliland Region"
replace ccode=353 if country_name=="Northern Cyprus"
replace ccode=775 if country_name=="Burma/Myanmar"
replace ccode=511 if country_name=="Zanzibar"
replace ccode=1005 if country_name=="Palestine/West Bank"
replace ccode=1006 if country_name=="Palestine/Gaza"
drop if ccode==.
gen cyear=ccode*10000+year
sort cyear

gen vdemelec=0
replace vdemelec=1 if v2x_regime==2
replace vdemelec=. if v2x_regime==.
label var vdemelec "elec democracy: v2x_regime>=2"

gen vdemlib = 0
replace vdemlib=1 if v2x_regime==3
replace vdemlib=. if v2x_regime==.
label var vdemlib "liberal democracy: v2x_regime=3"
bys year: egen mvdemlib = mean(vdemlib)

gen vdemdem = 0
replace vdemdem=1 if v2x_regime==3|v2x_regime==2
replace vdemdem=. if v2x_regime==.
label var vdemdem "vdem elec or lib democracy: v2x_regime=2 or 3"
bys year: egen mvdemdem = mean(vdemdem)

xtset country_id year
gen lv2caviol = l.v2caviol
gen lv2cacamps = l.v2cacamps
gen lv2xcivlib = l.v2x_civlib
bys year: egen mvpoly = mean(v2x_polyarchy)
bys year: egen mvlib = mean(v2x_libdem)

keep country_name year cyear ccode e_pop v2x_regime_amb lv2caviol v2caviol lv2cacamps v2cacamps lv2xcivlib v2caautmob v2x_polyarchy v2x_libdem v2x_liberal e_v2x_polyarchy_5C e_v2x_libdem_5C v2x_regime v2elmulpar_osp v2elfrfair_osp v2cltrnslw_osp v2cltrnslw_osp_codelow v2cltrnslw_osp_codehigh v2clacjstm_osp v2clacjstm_osp_codehigh v2clacjstm_osp_codelow v2clacjstw_osp v2clacjstw_osp_codehigh v2clacjstw_osp_codelow vdemlib vdemelec vdemdem v2x_regime e_regionpol v2cacamps_ord v2smpolsoc_ord mvdemdem v2x_civlib e_pt_coup e_miinteco e_coups v2elmulpar_osp_codehigh v2elfrfair_osp_codehigh v2x_polyarchy_codehigh v2x_liberal_codehigh
sort cyear
save "vdem12wk.dta", replace
keep  year cyear ccode vdemdem vdemlib
sort cyear
save "vdemshort.dta", replace


****************************************************************************
*VDEM Autocratization Episodes (Luhrman and Lindberg 2019)
****************************************************************************
import excel "llautocratization.xlsx", sheet("Sheet1") firstrow clear
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"

gen y=autendyr-autstartyr+1
expand y, gen(autyrsall)
sort ccode year
bys ccode year: gen sum = sum(autocratization)
replace year = year+sum-1
drop sum y
gen cyear=ccode*10000+year
sort cyear
save "llautocratization.dta", replace


*******************************************
*FREEDOM HOUSE DATA
*******************************************

import excel "Country_and_Territory_Ratings_and_Statuses_FIW_1973-2022.xlsx", sheet("Country Ratings, Statuses ") clear
rename A country
rename B PR1972
rename C CL1972
rename D STAT1972
rename E PR1973
rename F CL1973
rename G STAT1973
rename H PR1974
rename I CL1974
rename J STAT1974
rename K PR1975
rename L CL1975
rename M STAT1975
rename N PR1976
rename O CL1976
rename P STAT1976
rename Q PR1977
rename R CL1977
rename S STAT1977
rename T PR1978
rename U CL1978
rename V STAT1978
rename W PR1979
rename X CL1979
rename Y STAT1979
rename Z PR1980
rename AA CL1980
rename AB STAT1980
rename AC PR1981
rename AD CL1981
rename AE STAT1981
rename AF PR1983
rename AG CL1983
rename AH STAT1983
rename AI PR1984
rename AJ CL1984
rename AK STAT1984
rename AL PR1985
rename AM CL1985
rename AN STAT1985
rename AO PR1986
rename AP CL1986
rename AQ STAT1986
rename AR PR1987
rename AS CL1987
rename AT STAT1987
rename AU PR1988
rename AV CL1988
rename AW STAT1988
rename AX PR1989
rename AY CL1989
rename AZ STAT1989
rename BA PR1990
rename BB CL1990
rename BC STAT1990
rename BD PR1991
rename BE CL1991
rename BF STAT1991
rename BG PR1992
rename BH CL1992
rename BI STAT1992
rename BJ PR1993
rename BK CL1993
rename BL STAT1993
rename BM PR1994
rename BN CL1994
rename BO STAT1994
rename BP PR1995
rename BQ CL1995
rename BR STAT1995
rename BS PR1996
rename BT CL1996
rename BU STAT1996
rename BV PR1997
rename BW CL1997
rename BX STAT1997
rename BY PR1998
rename BZ CL1998
rename CA STAT1998
rename CB PR1999
rename CC CL1999
rename CD STAT1999
rename CE PR2000
rename CF CL2000
rename CG STAT2000
rename CH PR2001
rename CI CL2001
rename CJ STAT2001
rename CK PR2002
rename CL CL2002
rename CM STAT2002
rename CN PR2003
rename CO CL2003
rename CP STAT2003
rename CQ PR2004
rename CR CL2004
rename CS STAT2004
rename CT PR2005
rename CU CL2005
rename CV STAT2005
rename CW PR2006
rename CX CL2006
rename CY STAT2006
rename CZ PR2007
rename DA CL2007
rename DB STAT2007
rename DC PR2008
rename DD CL2008
rename DE STAT2008
rename DF PR2009
rename DG CL2009
rename DH STAT2009
rename DI PR2010
rename DJ CL2010
rename DK STAT2010
rename DL PR2011
rename DM CL2011
rename DN STAT2011
rename DO PR2012
rename DP CL2012
rename DQ STAT2012
rename DR PR2013
rename DS CL2013
rename DT STAT2013
rename DU PR2014
rename DV CL2014
rename DW STAT2014
rename DX PR2015
rename DY CL2015
rename DZ STAT2015
rename EA PR2016
rename EB CL2016
rename EC STAT2016
rename ED PR2017
rename EE CL2017
rename EF STAT2017
rename EG PR2018
rename EH CL2018
rename EI STAT2018
rename EJ PR2019
rename EK CL2019
rename EL STAT2019
rename EM PR2020
rename EN CL2020
rename EO STAT2020
rename EP PR2021
rename EQ CL2021
rename ER STAT2021



drop if _n==1|_n==2|_n==3

replace  PR1972 ="" if  PR1972=="-"
replace  CL1972 ="" if  CL1972=="-"
replace  STAT1972 ="" if  STAT1972=="-"
replace  PR1973 ="" if  PR1973=="-"
replace  CL1973 ="" if  CL1973=="-"
replace  STAT1973 ="" if  STAT1973=="-"
replace  PR1974 ="" if  PR1974=="-"
replace  CL1974 ="" if  CL1974=="-"
replace  STAT1974 ="" if  STAT1974=="-"
replace  PR1975 ="" if  PR1975=="-"
replace  CL1975 ="" if  CL1975=="-"
replace  STAT1975 ="" if  STAT1975=="-"
replace  PR1976 ="" if  PR1976=="-"
replace  CL1976 ="" if  CL1976=="-"
replace  STAT1976 ="" if  STAT1976=="-"
replace  PR1977 ="" if  PR1977=="-"
replace  CL1977 ="" if  CL1977=="-"
replace  STAT1977 ="" if  STAT1977=="-"
replace  PR1978 ="" if  PR1978=="-"
replace  CL1978 ="" if  CL1978=="-"
replace  STAT1978 ="" if  STAT1978=="-"
replace  PR1979 ="" if  PR1979=="-"
replace  CL1979 ="" if  CL1979=="-"
replace  STAT1979 ="" if  STAT1979=="-"
replace  PR1980 ="" if  PR1980=="-"
replace  CL1980 ="" if  CL1980=="-"
replace  STAT1980 ="" if  STAT1980=="-"
replace  PR1981 ="" if  PR1981=="-"
replace  CL1981 ="" if  CL1981=="-"
replace  STAT1981 ="" if  STAT1981=="-"
replace  PR1983 ="" if  PR1983=="-"
replace  CL1983 ="" if  CL1983=="-"
replace  STAT1983 ="" if  STAT1983=="-"
replace  PR1984 ="" if  PR1984=="-"
replace  CL1984 ="" if  CL1984=="-"
replace  STAT1984 ="" if  STAT1984=="-"
replace  PR1985 ="" if  PR1985=="-"
replace  CL1985 ="" if  CL1985=="-"
replace  STAT1985 ="" if  STAT1985=="-"
replace  PR1986 ="" if  PR1986=="-"
replace  CL1986 ="" if  CL1986=="-"
replace  STAT1986 ="" if  STAT1986=="-"
replace  PR1987 ="" if  PR1987=="-"
replace  CL1987 ="" if  CL1987=="-"
replace  STAT1987 ="" if  STAT1987=="-"
replace  PR1988 ="" if  PR1988=="-"
replace  CL1988 ="" if  CL1988=="-"
replace  STAT1988 ="" if  STAT1988=="-"
replace  PR1989 ="" if  PR1989=="-"
replace  CL1989 ="" if  CL1989=="-"
replace  STAT1989 ="" if  STAT1989=="-"
replace  PR1990 ="" if  PR1990=="-"
replace  CL1990 ="" if  CL1990=="-"
replace  STAT1990 ="" if  STAT1990=="-"
replace  PR1991 ="" if  PR1991=="-"
replace  CL1991 ="" if  CL1991=="-"
replace  STAT1991 ="" if  STAT1991=="-"
replace  PR1992 ="" if  PR1992=="-"
replace  CL1992 ="" if  CL1992=="-"
replace  STAT1992 ="" if  STAT1992=="-"
replace  PR1993 ="" if  PR1993=="-"
replace  CL1993 ="" if  CL1993=="-"
replace  STAT1993 ="" if  STAT1993=="-"
replace  PR1994 ="" if  PR1994=="-"
replace  CL1994 ="" if  CL1994=="-"
replace  STAT1994 ="" if  STAT1994=="-"
replace  PR1995 ="" if  PR1995=="-"
replace  CL1995 ="" if  CL1995=="-"
replace  STAT1995 ="" if  STAT1995=="-"
replace  PR1996 ="" if  PR1996=="-"
replace  CL1996 ="" if  CL1996=="-"
replace  STAT1996 ="" if  STAT1996=="-"
replace  PR1997 ="" if  PR1997=="-"
replace  CL1997 ="" if  CL1997=="-"
replace  STAT1997 ="" if  STAT1997=="-"
replace  PR1998 ="" if  PR1998=="-"
replace  CL1998 ="" if  CL1998=="-"
replace  STAT1998 ="" if  STAT1998=="-"
replace  PR1999 ="" if  PR1999=="-"
replace  CL1999 ="" if  CL1999=="-"
replace  STAT1999 ="" if  STAT1999=="-"
replace  PR2000 ="" if  PR2000=="-"
replace  CL2000 ="" if  CL2000=="-"
replace  STAT2000 ="" if  STAT2000=="-"
replace  PR2001 ="" if  PR2001=="-"
replace  CL2001 ="" if  CL2001=="-"
replace  STAT2001 ="" if  STAT2001=="-"
replace  PR2002 ="" if  PR2002=="-"
replace  CL2002 ="" if  CL2002=="-"
replace  STAT2002 ="" if  STAT2002=="-"
replace  PR2003 ="" if  PR2003=="-"
replace  CL2003 ="" if  CL2003=="-"
replace  STAT2003 ="" if  STAT2003=="-"
replace  PR2004 ="" if  PR2004=="-"
replace  CL2004 ="" if  CL2004=="-"
replace  STAT2004 ="" if  STAT2004=="-"
replace  PR2005 ="" if  PR2005=="-"
replace  CL2005 ="" if  CL2005=="-"
replace  STAT2005 ="" if  STAT2005=="-"
replace  PR2006 ="" if  PR2006=="-"
replace  CL2006 ="" if  CL2006=="-"
replace  STAT2006 ="" if  STAT2006=="-"
replace  PR2007 ="" if  PR2007=="-"
replace  CL2007 ="" if  CL2007=="-"
replace  STAT2007 ="" if  STAT2007=="-"
replace  PR2008 ="" if  PR2008=="-"
replace  CL2008 ="" if  CL2008=="-"
replace  STAT2008 ="" if  STAT2008=="-"
replace  PR2009 ="" if  PR2009=="-"
replace  CL2009 ="" if  CL2009=="-"
replace  STAT2009 ="" if  STAT2009=="-"
replace  PR2010 ="" if  PR2010=="-"
replace  CL2010 ="" if  CL2010=="-"
replace  STAT2010 ="" if  STAT2010=="-"
replace  PR2011 ="" if  PR2011=="-"
replace  CL2011 ="" if  CL2011=="-"
replace  STAT2011 ="" if  STAT2011=="-"
replace  PR2012 ="" if  PR2012=="-"
replace  CL2012 ="" if  CL2012=="-"
replace  STAT2012 ="" if  STAT2012=="-"
replace  PR2013 ="" if  PR2013=="-"
replace  CL2013 ="" if  CL2013=="-"
replace  STAT2013 ="" if  STAT2013=="-"
replace  PR2014 ="" if  PR2014=="-"
replace  CL2014 ="" if  CL2014=="-"
replace  STAT2014 ="" if  STAT2014=="-"
replace  PR2015 ="" if  PR2015=="-"
replace  CL2015 ="" if  CL2015=="-"
replace  STAT2015 ="" if  STAT2015=="-"
replace  PR2016 ="" if  PR2016=="-"
replace  CL2016 ="" if  CL2016=="-"
replace  STAT2016 ="" if  STAT2016=="-"
replace  PR2017 ="" if  PR2017=="-"
replace  CL2017 ="" if  CL2017=="-"
replace  STAT2017 ="" if  STAT2017=="-"
replace  PR2018 ="" if  PR2018=="-"
replace  CL2018 ="" if  CL2018=="-"
replace  STAT2018 ="" if  STAT2018=="-"
replace  PR2019 ="" if  PR2019=="-"
replace  CL2019 ="" if  CL2019=="-"
replace  STAT2019 ="" if  STAT2019=="-"
replace  PR2020 ="" if  PR2020=="-"
replace  CL2020 ="" if  CL2020=="-"
replace  STAT2020 ="" if  STAT2020=="-"
replace  PR2021 ="" if  PR2021=="-"
replace  CL2021 ="" if  CL2021=="-"
replace  STAT2021 ="" if  STAT2021=="-"


replace PR1972="5" if country=="South Africa"
replace CL1972="6" if country=="South Africa"

destring  CL1972, replace
destring  CL1973, replace
destring  CL1974, replace
destring  CL1975, replace
destring  CL1976, replace
destring  CL1977, replace
destring  CL1978, replace
destring  CL1979, replace
destring  CL1980, replace
destring  CL1981, replace
destring  CL1983, replace
destring  CL1984, replace
destring  CL1985, replace
destring  CL1986, replace
destring  CL1987, replace
destring  CL1988, replace
destring  CL1989, replace
destring  CL1990, replace
destring  CL1991, replace
destring  CL1992, replace
destring  CL1993, replace
destring  CL1994, replace
destring  CL1995, replace
destring  CL1996, replace
destring  CL1997, replace
destring  CL1998, replace
destring  CL1999, replace
destring  CL2000, replace
destring  CL2001, replace
destring  CL2002, replace
destring  CL2003, replace
destring  CL2004, replace
destring  CL2005, replace
destring  CL2006, replace
destring  CL2007, replace
destring  CL2008, replace
destring  CL2009, replace
destring  CL2010, replace
destring  CL2011, replace
destring  CL2012, replace
destring  CL2013, replace
destring  CL2014, replace
destring  CL2015, replace
destring  CL2016, replace
destring  CL2017, replace
destring  CL2018, replace
destring  CL2019, replace
destring  CL2020, replace
destring  CL2021, replace
destring  PR1972, replace
destring  PR1973, replace
destring  PR1974, replace
destring  PR1975, replace
destring  PR1976, replace
destring  PR1977, replace
destring  PR1978, replace
destring  PR1979, replace
destring  PR1980, replace
destring  PR1981, replace
destring  PR1983, replace
destring  PR1984, replace
destring  PR1985, replace
destring  PR1986, replace
destring  PR1987, replace
destring  PR1988, replace
destring  PR1989, replace
destring  PR1990, replace
destring  PR1991, replace
destring  PR1992, replace
destring  PR1993, replace
destring  PR1994, replace
destring  PR1995, replace
destring  PR1996, replace
destring  PR1997, replace
destring  PR1998, replace
destring  PR1999, replace
destring  PR2000, replace
destring  PR2001, replace
destring  PR2002, replace
destring  PR2003, replace
destring  PR2004, replace
destring  PR2005, replace
destring  PR2006, replace
destring  PR2007, replace
destring  PR2008, replace
destring  PR2009, replace
destring  PR2010, replace
destring  PR2011, replace
destring  PR2012, replace
destring  PR2013, replace
destring  PR2014, replace
destring  PR2015, replace
destring  PR2016, replace
destring  PR2017, replace
destring  PR2018, replace
destring  PR2019, replace
destring  PR2020, replace
destring  PR2021, replace

gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if ccode==.

*there are errors in Freedom House's data file for 2021. I have corrected using FH's other file at: https://freedomhouse.org/sites/default/files/2022-02/Aggregate_Category_and_Subcategory_Scores_FIW_2003-2022.xlsx

save "fh20.dta", replace

drop STAT* CL*
*combine some duplicates
collapse (mean) PR*, by(ccode)
reshape long PR, i(ccode) j(year)
rename PR fhpol
xtset ccode year
gen fhpolnr = 8-fhpol
gen lfhpolnr = l.fhpolnr
gen cyear=ccode*10000+year
sort cyear
save "fh20pollong.dta", replace
keep ccode year cyear fhpol fhpolnr lfhpolnr
sort cyear
save "fh20polshort.dta", replace
use "fh20.dta", clear
drop STAT* PR*
*combine some duplicates
collapse (mean) CL*, by(ccode)
reshape long CL, i(ccode) j(year)
rename CL fhciv
xtset ccode year
gen cyear=ccode*10000+year
sort cyear
save "fh20civlong.dta", replace
use "fh20.dta", clear
drop CL* PR*
*combine some duplicates
collapse (firstnm) STAT*, by(ccode)
reshape long STAT, i(ccode) j(year)
rename STAT fhstat
xtset ccode year
gen cyear=ccode*10000+year
sort cyear
save "fh20statlong.dta", replace

*For the 100 point FH political scores
import excel "Aggregate_Category_and_Subcategory_Scores_FIW_2003-2022 (1).xlsx", sheet("FIW06-22") firstrow clear
rename CountryTerritory country
gen year = Edition-1
keep country year PR CL
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="Germany, W. "|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="Germany, E. "|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="Eswatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="Yemen, N."|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="Yemen, S."|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, S."|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam, N."
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if ccode==.
xtset ccode year
gen lPR = l.PR
gen cyear=ccode*10000+year
sort cyear
save "FHPRCL.dta", replace


*********************************************************************************************
*MADDISON 2020 DATA (https://www.rug.nl/ggdc/historicaldevelopment/maddison/data/mpd2020.dta)
*********************************************************************************************

use "mad2020.dta", clear
replace gdppc=. if gdppc==0
drop if countrycode=="SUN"&year>1949
*doubling up of FSU and Russia
drop if country=="Serbia"
*use Former Yugo instead
sort country year
*bys country: ipolate gdppc year, gen(gdppci)
save "mad2020wk.dta", replace
gen ccode = .
replace ccode = 700 if country=="Afghanistan"
replace ccode = 339 if country=="Albania"
replace ccode = 615 if country=="Algeria"
replace ccode = 540 if country=="Angola"
replace ccode = 160 if country=="Argentina"
replace ccode = 371 if country=="Armenia"
replace ccode = 900 if country=="Australia"
replace ccode = 305 if country=="Austria"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 692 if country=="Bahrain"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 370 if country=="Belarus"
replace ccode = 211 if country=="Belgium"
replace ccode = 434 if country=="Benin"
replace ccode = 145 if country=="Bolivia"
replace ccode = 346 if country=="Bosnia and Herzegovina"
replace ccode = 571 if country=="Botswana"
replace ccode = 140 if country=="Brazil"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 775 if country=="Myanmar"
replace ccode = 516 if country=="Burundi"
replace ccode = 811 if country=="Cambodia"
replace ccode = 471 if country=="Cameroon"
replace ccode = 20 if country=="Canada"
replace ccode = 402 if country=="Cabo Verde"
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 155 if country=="Chile"
replace ccode = 710 if country=="China"
replace ccode = 100 if country=="Colombia"
replace ccode = 581 if country=="Comoros"
replace ccode = 484 if country=="Congo"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 437 if country=="C𴥠d'Ivoire"
replace ccode = 437 if country=="Côte d'Ivoire"
replace ccode = 344 if country=="Croatia"
replace ccode = 40 if country=="Cuba"
replace ccode = 316 if country=="Czech Republic"
replace ccode = 315 if country=="Czechoslovakia"
replace ccode = 390 if country=="Denmark"
replace ccode = 522 if country=="Djibouti"
replace ccode = 42 if country=="Dominican Republic"
replace ccode = 130 if country=="Ecuador"
replace ccode = 651 if country=="Egypt"
replace ccode = 92 if country=="El Salvador"
replace ccode = 411 if country=="Equatorial Guinea"
replace ccode = 531 if country=="Eritrea"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 366 if country=="Estonia"
replace ccode = 375 if country=="Finland"
replace ccode = 220 if country=="France"
replace ccode = 481 if country=="Gabon"
replace ccode = 420 if country=="Gambia"
replace ccode = 372 if country=="Georgia"
replace ccode = 255 if country=="Germany"
replace ccode = 452 if country=="Ghana"
replace ccode = 350 if country=="Greece"
replace ccode = 90 if country=="Guatemala"
replace ccode = 438 if country=="Guinea"
replace ccode = 404 if country=="Guinea-Bissau"
replace ccode = 110 if country=="Guyana"
replace ccode = 41 if country=="Ha൩"
replace ccode = 41 if country=="Haiti"
replace ccode = 91 if country=="Honduras"
replace ccode = 1001 if country=="China, Hong Kong SAR"
replace ccode = 310 if country=="Hungary"
replace ccode = 750 if country=="India"
replace ccode = 850 if country=="Indonesia"
replace ccode = 630 if country=="Iran (Islamic Republic of)"
replace ccode = 645 if country=="Iraq"
replace ccode = 205 if country=="Ireland"
replace ccode = 666 if country=="Israel"
replace ccode = 325 if country=="Italy"
replace ccode = 51 if country=="Jamaica"
replace ccode = 740 if country=="Japan"
replace ccode = 663 if country=="Jordan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 501 if country=="Kenya"
replace ccode = 347 if country=="Kosovo"
replace ccode = 690 if country=="Kuwait"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 812 if country=="Lao People's DR"
replace ccode = 367 if country=="Latvia"
replace ccode = 660 if country=="Lebanon"
replace ccode = 570 if country=="Lesotho"
replace ccode = 450 if country=="Liberia"
replace ccode = 620 if country=="Libya"
replace ccode = 368 if country=="Lithuania"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 343 if country=="TFYR of Macedonia"
replace ccode = 580 if country=="Madagascar"
replace ccode = 553 if country=="Malawi"
replace ccode = 820 if country=="Malaysia"
replace ccode = 338 if country=="Malta"
replace ccode = 432 if country=="Mali"
replace ccode = 435 if country=="Mauritania"
replace ccode = 590 if country=="Mauritius"
replace ccode = 70 if country=="Mexico"
replace ccode = 359 if country=="Republic of Moldova"
replace ccode = 712 if country=="Mongolia"
replace ccode = 341 if country=="Montenegro"
replace ccode = 600 if country=="Morocco"
replace ccode = 541 if country=="Mozambique"
replace ccode = 565 if country=="Namibia"
replace ccode = 790 if country=="Nepal"
replace ccode = 210 if country=="Netherlands"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 436 if country=="Niger"
replace ccode = 475 if country=="Nigeria"
replace ccode = 731 if country=="D.P.R. of Korea"
replace ccode = 385 if country=="Norway"
replace ccode = 698 if country=="Oman"
replace ccode = 770 if country=="Pakistan"
replace ccode = 95 if country=="Panama"
replace ccode = 150 if country=="Paraguay"
replace ccode = 135 if country=="Peru"
replace ccode = 840 if country=="Philippines"
replace ccode = 290 if country=="Poland"
replace ccode = 235 if country=="Portugal"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 694 if country=="Qatar"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russian Federation"&year>1949|country=="Former USSR"
replace ccode = 517 if country=="Rwanda"
replace ccode = 403 if country=="S䯠Tom顡nd Principe"
replace ccode = 403 if country=="Sao Tome and Principe"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 433 if country=="Senegal"
replace ccode = 342 if country=="Serbia"
replace ccode = 348 if country=="Serbia and Montenegro"
replace ccode=345 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode = 591 if country=="Seychelles"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 830 if country=="Singapore"
replace ccode = 317 if country=="Slovakia"
replace ccode = 349 if country=="Slovenia"
replace ccode = 520 if country=="Somalia"
replace ccode = 560 if country=="South Africa"
replace ccode = 732 if country=="Republic of Korea"
replace ccode = 730 if country=="Korea"
replace ccode = 230 if country=="Spain"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 625 if country=="Sudan (Former)"
replace ccode = 572 if country=="Swaziland"
replace ccode = 380 if country=="Sweden"
replace ccode = 225 if country=="Switzerland"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 713 if country=="Taiwan, Province of China"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 510 if country=="U.R. of Tanzania: Mainland"
replace ccode = 800 if country=="Thailand"
replace ccode = 461 if country=="Togo"
replace ccode = 365 if country=="Total Former USSR"&year<1992
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 616 if country=="Tunisia"
replace ccode = 640 if country=="Turkey"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 500 if country=="Uganda"
replace ccode = 369 if country=="Ukraine"
replace ccode = 696 if country=="United Arab Emirates"
replace ccode = 200 if country=="United Kingdom"
replace ccode = 2 if country=="United States"
replace ccode = 165 if country=="Uruguay"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 101 if country=="Venezuela (Bolivarian Republic of)"
replace ccode = 818 if country=="Viet Nam"
replace ccode = 816 if country=="Vietnam North"
replace ccode = 817 if country=="Vietnam South"
replace ccode = 1005 if country=="West Bank and Gaza"|country=="State of Palestine"
replace ccode = 679 if country=="Yemen"
replace ccode = 678 if country=="Yemen North"
replace ccode = 680 if country=="Yemen South"
replace ccode = 342 if country=="Former Yugoslavia"
replace ccode = 490 if country=="D.R. of the Congo"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 395 if country=="Iceland"
replace ccode = 54 if country=="Dominica"
replace ccode = 352 if country=="Cyprus"
replace ccode = 145 if country=="Bolivia (Plurinational State of)"
replace ccode = 53 if country=="Barbados"
drop if ccode==.
gen cyear=ccode*10000+year
xtset ccode year 
gen madgrowth=((gdppci/l.gdppci)-1)*100
label var madgrowth "growth rate of gdppc; Maddison 2020"
gen lngdppci = ln(gdppci)
label var lngdppci "ln of gdppci; Maddison 2020"
keep country year gdppc gdppci lngdppci pop ccode cyear madgrowth
drop if year<1800
sort cyear
save "mad2020wk.dta", replace


**********************************************************************************************************************
*LEXICAL INDEX OF ELECTORAL DEMOCRACY (https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/29106)
**********************************************************************************************************************

*i IMPORTED LIED DATA FROM: "https://ps.au.dk/fileadmin/Statskundskab/Dokumenter/Forskning/Forskningscentre/DEDERE/lied_v6.xls"
import excel "lied_v6.0.xls", sheet("Ark1") firstrow clear
*create id code
rename cow ccode
*Clean up
*Tibet
drop if ccode==711
replace ccode=255 if ccode==260
*adjust to my country codings* 
drop if ccode==260&year==1945
*not to have both Germany and FRG in 1945*
drop if ccode==260&year==1990
*not to have both Germany and FRG in 1990*
drop if ccode==347&year==1991
*combine Yugoslavia pre 2003 with Serbia and Montenegro 2003-2006
replace ccode = 342 if country=="Yugoslavia"
replace ccode=342 if country=="Serbia and Montenegro"&year>2002&year<2007
replace ccode = 341 if country=="Montenegro"
replace ccode=342 if country=="Serbia"
*Vietnam
replace ccode=818 if ccode==816
*South Sudan
replace ccode=626 if country=="South Sudan"
replace ccode=626 if country=="South Sudan"
drop if ccode==.
*note this drops various colonies that were not sovereign states.
duplicates drop
gen cyear=.
replace cyear=ccode*10000+year
sort cyear
gen lied6 = 0
replace lied6=1 if lexical_index==6
replace lied6=. if lexical_index==.
bys year: egen proplied6=mean(lied6)
label var proplied6 "proportion of countries for which LIED = 6"

gen lied5 = 0
replace lied5=1 if lexical_index==5|lexical_index==6
replace lied5=. if lexical_index==.
bys year: egen proplied5=mean(lied5)
label var proplied5 "proportion of countries for which LIED >= 5"
xtset ccode year
gen liedbkd=0
replace liedbkd=1 if l.lexical_index==6&lexical_index<6
replace liedbkd=. if l.lexical_index==.|lexical_index==.
bys year: egen mliedbkd = mean(liedbkd)
xtset ccode year
gen lmliedbkd = l.mliedbkd
gen l2mliedbkd = l2.mliedbkd
gen ldmliedbkd=lmliedbkd-l2mliedbkd

sort cyear
save "lied_v3wk.dta", replace


**********************************************************************************************************************
*Get SWIID V6.2 data from https://dataverse.harvard.edu/file.xhtml?fileId=3136167&version=RELEASED&version=.0
*Generate averages of inequality imputed estimates
**********************************************************************************************************************
use "swiid9_0.dta", clear
egen avginidisp = rowmean(_1_gini_disp _2_gini_disp _3_gini_disp _4_gini_disp _5_gini_disp _6_gini_disp _7_gini_disp _8_gini_disp _9_gini_disp _10_gini_disp _11_gini_disp _12_gini_disp _13_gini_disp _14_gini_disp _15_gini_disp _16_gini_disp _17_gini_disp _18_gini_disp _19_gini_disp _20_gini_disp _21_gini_disp _22_gini_disp _23_gini_disp _24_gini_disp _25_gini_disp _26_gini_disp _27_gini_disp _28_gini_disp _29_gini_disp _30_gini_disp _31_gini_disp _32_gini_disp _33_gini_disp _34_gini_disp _35_gini_disp _36_gini_disp _37_gini_disp _38_gini_disp _39_gini_disp _40_gini_disp _41_gini_disp _42_gini_disp _43_gini_disp _44_gini_disp _45_gini_disp _46_gini_disp _47_gini_disp _48_gini_disp _49_gini_disp _50_gini_disp _51_gini_disp _52_gini_disp _53_gini_disp _54_gini_disp _55_gini_disp _56_gini_disp _57_gini_disp _58_gini_disp _59_gini_disp _60_gini_disp _61_gini_disp _62_gini_disp _63_gini_disp _64_gini_disp _65_gini_disp _66_gini_disp _67_gini_disp _68_gini_disp _69_gini_disp _70_gini_disp _71_gini_disp _72_gini_disp _73_gini_disp _74_gini_disp _75_gini_disp _76_gini_disp _77_gini_disp _78_gini_disp _79_gini_disp _80_gini_disp _81_gini_disp _82_gini_disp _83_gini_disp _84_gini_disp _85_gini_disp _86_gini_disp _87_gini_disp _88_gini_disp _89_gini_disp _90_gini_disp _91_gini_disp _92_gini_disp _93_gini_disp _94_gini_disp _95_gini_disp _96_gini_disp _97_gini_disp _98_gini_disp _99_gini_disp _100_gini_disp)
egen avginimkt = rowmean(_1_gini_mkt _2_gini_mkt _3_gini_mkt _4_gini_mkt _5_gini_mkt _6_gini_mkt _7_gini_mkt _8_gini_mkt _9_gini_mkt _10_gini_mkt _11_gini_mkt _12_gini_mkt _13_gini_mkt _14_gini_mkt _15_gini_mkt _16_gini_mkt _17_gini_mkt _18_gini_mkt _19_gini_mkt _20_gini_mkt _21_gini_mkt _22_gini_mkt _23_gini_mkt _24_gini_mkt _25_gini_mkt _26_gini_mkt _27_gini_mkt _28_gini_mkt _29_gini_mkt _30_gini_mkt _31_gini_mkt _32_gini_mkt _33_gini_mkt _34_gini_mkt _35_gini_mkt _36_gini_mkt _37_gini_mkt _38_gini_mkt _39_gini_mkt _40_gini_mkt _41_gini_mkt _42_gini_mkt _43_gini_mkt _44_gini_mkt _45_gini_mkt _46_gini_mkt _47_gini_mkt _48_gini_mkt _49_gini_mkt _50_gini_mkt _51_gini_mkt _52_gini_mkt _53_gini_mkt _54_gini_mkt _55_gini_mkt _56_gini_mkt _57_gini_mkt _58_gini_mkt _59_gini_mkt _60_gini_mkt _61_gini_mkt _62_gini_mkt _63_gini_mkt _64_gini_mkt _65_gini_mkt _66_gini_mkt _67_gini_mkt _68_gini_mkt _69_gini_mkt _70_gini_mkt _71_gini_mkt _72_gini_mkt _73_gini_mkt _74_gini_mkt _75_gini_mkt _76_gini_mkt _77_gini_mkt _78_gini_mkt _79_gini_mkt _80_gini_mkt _81_gini_mkt _82_gini_mkt _83_gini_mkt _84_gini_mkt _85_gini_mkt _86_gini_mkt _87_gini_mkt _88_gini_mkt _89_gini_mkt _90_gini_mkt _91_gini_mkt _92_gini_mkt _93_gini_mkt _94_gini_mkt _95_gini_mkt _96_gini_mkt _97_gini_mkt _98_gini_mkt _99_gini_mkt _100_gini_mkt)
mi unset
keep country year avginidisp avginimkt
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"
replace ccode = 265 if country=="GDR"|country=="East Germany"|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="Macedonia (FYROM)"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country=="Congo, D.R."
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, Dem. R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="North Yemen"|country=="Yemen (AR)"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="South Yemen"|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, South"|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"
drop if country=="Czech Republic"&year==1988
drop if country=="Czech Republic"&year==1989
drop if country=="Russia"&year==1988
drop if country=="Russia"&year==1989
drop if country=="Russia"&year==1990
gen cyear=ccode*10000+year
sort cyear
save "swiid9_0work.dta", replace



******************************************************************************
* DATABASE OF POLITICAL INSTITUTIONS (DPI)
******************************************************************************
use "DPI2020.dta", clear
gen ccode=.
replace ccode = 100 if country=="Colombia"
replace ccode = 1001 if country=="Hong Kong"|country=="China: Hong Kong SAR"|country=="Hong Kong SAR"|country=="Hong Kong SAR, China"|country=="China, Hong Kong SAR"|country=="Hong Kong, China"
replace ccode = 1002 if country=="Puerto Rico"
replace ccode = 1005 if country=="Palestine"|country=="State of Palestine"|country=="Palestinian Territories"|country=="West Bank and Gaza Strip"|country=="West Bank and Gaza"|country=="Israeli-Occupied Territories and Palestinian Authority"|country=="Palestinian Authority"|country=="West Bank"|country=="Palestine, State of"
replace ccode = 101 if country=="Venezuela"|country=="Venezuela (Bolivarian Republic of)"|country=="Venezuela, RB"|country=="Venezuela, Bolivarian Republic of"
replace ccode = 110 if country=="Guyana"
replace ccode = 115 if country=="Suriname"
replace ccode = 130 if country=="Ecuador"
replace ccode = 135 if country=="Peru"
replace ccode = 140 if country=="Brazil"
replace ccode = 145 if country=="Bolivia"|country=="Bolivia (Plurinational State of)"|country=="Bolivia, Plurinational State of"
replace ccode = 150 if country=="Paraguay"
replace ccode = 155 if country=="Chile"
replace ccode = 160 if country=="Argentina"
replace ccode = 165 if country=="Uruguay"
replace ccode = 166 if country=="Faeroe Islands"
replace ccode = 2 if country=="United States of America"|country=="United States"|country=="USA"|country=="US"
replace ccode = 20 if country=="Canada"
replace ccode = 200 if country=="Great Britain"|country=="United Kingdom"|country=="UK"
replace ccode = 201 if country=="Channel Islands"
replace ccode = 202 if country=="Isle of Man"
replace ccode = 205 if country=="Ireland"
replace ccode = 210 if country=="Netherlands"
replace ccode = 211 if country=="Belgium"
replace ccode = 212 if country=="Luxembourg"
replace ccode = 220 if country=="France"
replace ccode = 221 if country=="Monaco"
replace ccode = 223 if country=="Liechtenstein"
replace ccode = 225 if country=="Switzerland"
replace ccode = 230 if country=="Spain"
replace ccode = 232 if country=="Andorra"
replace ccode = 235 if country=="Portugal"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"|country=="Germany West"
replace ccode = 265 if country=="GDR"|country=="East Germany"|country=="Germany, East"|country=="East Germany (GDR)"|country=="Germany East"|country=="German Democratic Republic"
replace ccode = 290 if country=="Poland"
replace ccode = 305 if country=="Austria"
replace ccode = 31 if country=="Bahamas"
replace ccode = 31 if country=="Bahamas"|country=="Bahamas, The"|country=="The Bahamas"
replace ccode = 310 if country=="Hungary"
replace ccode = 316 if country=="Czech Rep."|country=="Czech Republic"|country=="Czechia"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 317 if country=="Slovak Republic"|country=="Slovakia"|country=="Slovak Rep."
replace ccode = 32 if country=="Aruba"
replace ccode = 325 if country=="Italy"
replace ccode = 33 if country=="Bermuda"
replace ccode = 331 if country=="San Marino"
replace ccode = 338 if country=="Malta"
replace ccode = 339 if country=="Albania"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Serbia"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="North Macedonia"
replace ccode = 344 if country=="Croatia"
replace ccode = 346 if country=="Bosnia and Herzegovina"|country=="Bosnia"|country=="Bosnia-Herz"|country=="Bosnia-Herzegovina"|country=="Bosnia & Herzegovina"
replace ccode = 347 if country=="Kosovo"
replace ccode = 349 if country=="Slovenia"
replace ccode = 350 if country=="Greece"
replace ccode = 352 if country=="Cyprus"|country=="Cyprus (Greek)"
replace ccode = 355 if country=="Bulgaria"
replace ccode = 359 if country=="Moldova"|country=="Republic of Moldova"|country=="Moldova, Republic of"
replace ccode = 360 if country=="Romania"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 366 if country=="Estonia"
replace ccode = 367 if country=="Latvia"
replace ccode = 368 if country=="Lithuania"
replace ccode = 369 if country=="Ukraine"
replace ccode = 370 if country=="Belarus"
replace ccode = 371 if country=="Armenia"
replace ccode = 372 if country=="Georgia"
replace ccode = 373 if country=="Azerbaijan"
replace ccode = 375 if country=="Finland"
replace ccode = 380 if country=="Sweden"
replace ccode = 385 if country=="Norway"
replace ccode = 390 if country=="Denmark"
replace ccode = 395 if country=="Iceland"
replace ccode = 396 if country=="Greenland"
replace ccode = 40 if country=="Cuba"
replace ccode = 402 if country=="C. Verde Is."|country=="Cabo Verde"|country=="Cape Verde"
replace ccode = 403 if country=="Sao Tome and Principe"|country=="São Tomé and Príncipe"|country=="Sao Tome"|country=="S. Tomé & Principe"
replace ccode = 404 if country=="Guinea-Bissau"|country=="Guinea Bissau"
replace ccode = 41 if country=="Haiti"
replace ccode = 411 if country=="Eq. Guinea"|country=="Equatorial Guinea"
replace ccode = 42 if country=="Dom. Rep."|country=="Dominican Rep."|country=="Dominican Republic"|country=="Dominican Rep"
replace ccode = 420 if country=="Gambia"|country=="Gambia, The"|country=="The Gambia"
replace ccode = 432 if country=="Mali"
replace ccode = 433 if country=="Senegal"
replace ccode = 434 if country=="Benin"
replace ccode = 435 if country=="Mauritania"
replace ccode = 436 if country=="Niger"
replace ccode = 437 if country=="C?te d'Ivoire"|country=="Cote d`Ivoire"|country=="Côte d´Ivoire"|country=="Côte d’Ivoire"|country=="Côte d'Ivoire"|country=="Cote d'Ivoire"|country=="Ivory Coast (Cote d'Ivoire)"|country=="Ivory Coast"|country=="Côte d'Ivoire"|country=="Côte D'Ivoire"|country=="Cote D'Ivoire"
replace ccode = 438 if country=="Guinea"
replace ccode = 439 if country=="Burkina Faso"
replace ccode = 450 if country=="Liberia"
replace ccode = 451 if country=="Sierra Leone"
replace ccode = 452 if country=="Ghana"
replace ccode = 461 if country=="Togo"
replace ccode = 471 if country=="Cameroon"
replace ccode = 475 if country=="Nigeria"
replace ccode = 481 if country=="Gabon"
replace ccode = 482 if country=="Cent. Af. Rep."
replace ccode = 482 if country=="Central Afr. Rep."|country=="Cen African Rep"|country=="Central African Rep."
replace ccode = 482 if country=="Central African Republic"
replace ccode = 483 if country=="Chad"
replace ccode = 484 if country=="Congo (Brazzaville)"
replace ccode = 484 if country=="Congo Brazzaville"|country=="Congo-Brz"
replace ccode = 484 if country=="Congo Republic"
replace ccode = 484 if country=="Congo"
replace ccode = 484 if country=="Congo, Rep. Of"
replace ccode = 484 if country=="Congo, Rep."|country=="Congo-Brazzaville"|country=="Republic of Congo"|country=="Congo, Republic"|country=="Republic of the Congo"
replace ccode = 484 if country=="Congo, Republic of"|country=="Congo, Republic of (Brazzaville)"
replace ccode = 484 if country=="Congo, Republicof"|country=="Congo (Rep.)"
replace ccode = 490 if country=="Congo (Democratic Republic of the)"|country=="Congo, the Democratic Republic of the"|country=="Congo (Dem. Rep.)"|country==" Congo, D.R."|country=="Congo Kinshasa"
replace ccode = 490 if country=="Congo (DRC)"|country=="Congo, Democratic Republic of (Kinshasa)"|country=="Zaire (Democ Republic Congo)"|country=="Congo/Zaire"
replace ccode = 490 if country=="Congo (Kinshasa)"|country=="Congo-Kinshasa"
replace ccode = 490 if country=="Congo, D.R."
replace ccode = 490 if country=="Congo, Dem. Rep."|country=="Congo, Democratic Republic"|country=="Democratic Republic of Congo"|country=="Zaire"
replace ccode = 490 if country=="Congo, Democratic Republic of"|country=="Congo, Dem. Rep."|country=="D.R. of the Congo"
replace ccode = 490 if country=="Democratic Republic of the Congo"
replace ccode = 500 if country=="Uganda"
replace ccode = 501 if country=="Kenya"
replace ccode = 51 if country=="Jamaica"
replace ccode = 510 if country=="Tanzania (United Republic of)"|country=="U.R. of Tanzania: Mainland"|country=="Tanzania, United Republic of"
replace ccode = 510 if country=="Tanzania"|country=="United Republic of Tanzania"
replace ccode = 516 if country=="Burundi"
replace ccode = 517 if country=="Rwanda"
replace ccode = 52 if country=="Trinidad & Tob."
replace ccode = 52 if country=="Trinidad & Tobago"
replace ccode = 52 if country=="Trinidad and Tobago"
replace ccode = 52 if country=="Trinidad-Tobago"
replace ccode = 520 if country=="Somalia"
replace ccode = 522 if country=="Djibouti"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 531 if country=="Eritrea"
replace ccode = 54 if country=="Dominica"
replace ccode = 540 if country=="Angola"
replace ccode = 541 if country=="Mozambique"
replace ccode = 55 if country=="Grenada"
replace ccode = 551 if country=="Zambia"
replace ccode = 552 if country=="Zimbabwe"|country=="Rhodesia"
replace ccode = 553 if country=="Malawi"
replace ccode = 56 if country=="Saint Lucia"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 560 if country=="S. Africa"
replace ccode = 560 if country=="South Africa"
replace ccode = 565 if country=="Namibia"
replace ccode = 57 if country=="Saint Vincent and the Grenadines"|country=="St Vincent"
replace ccode = 57 if country=="St. Vincent & Grenadines"
replace ccode = 57 if country=="St. Vincent and Grenadines"
replace ccode = 57 if country=="St. Vincent and the Grenadines"
replace ccode = 570 if country=="Lesotho"
replace ccode = 571 if country=="Botswana"
replace ccode = 572 if country=="Swaziland"|country=="eSwatini"
replace ccode = 58 if country=="Antigua and Barbuda"|country=="Antigua & Barbuda"
replace ccode = 580 if country=="Madagascar"
replace ccode = 581 if country=="Comoro Is."
replace ccode = 581 if country=="Comoros"
replace ccode = 59 if country=="Cayman Islands"
replace ccode = 590 if country=="Mauritius"
replace ccode = 591 if country=="Seychelles"
replace ccode = 60 if country=="Saint Kitts & Nevis"|country=="Saint Kitts and Nevis"|country=="St Kitts"
replace ccode = 60 if country=="St. Kitts and Nevis"
replace ccode = 60 if country=="St.Kitts & Nevis"
replace ccode = 600 if country=="Morocco"
replace ccode = 61 if country=="Curacao"
replace ccode = 615 if country=="Algeria"
replace ccode = 616 if country=="Tunisia"
replace ccode = 62 if country=="Turks and Caicos Islands"
replace ccode = 620 if country=="Libya"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 63 if country=="Virgin Islands (U.S.)"
replace ccode = 630 if country=="Iran (Islamic Republic of)"|country=="Iran, Islamic Republic of"
replace ccode = 630 if country=="Iran"|country=="Iran (I.R.)"
replace ccode = 630 if country=="Iran"
replace ccode = 630 if country=="Iran, Islamic Rep."|country=="Islamic Republic of Iran"
replace ccode = 640 if country=="Turkey"
replace ccode = 645 if country=="Iraq"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt"
replace ccode = 651 if country=="Egypt, Arab Rep."
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syria"
replace ccode = 652 if country=="Syrian Arab Republic"
replace ccode = 660 if country=="Lebanon"
replace ccode = 663 if country=="Jordan"
replace ccode = 666 if country=="Israel"|country=="Israel and Occupied Territories**"|country=="Israel in pre-1967 borders"
replace ccode = 670 if country=="Saudi Arabia"
replace ccode = 678 if country=="Yemen Arab Republic"|country=="Yemen (A.R.)"|country=="North Yemen"|country=="Yemen (AR)"|country=="Yemen North"
replace ccode = 679 if country=="Yemen, Rep."|country=="Yemen"|country=="Yemen, Republic of"
replace ccode = 680 if country=="Yemen (PDR)"|country=="South Yemen"|country=="Yemen, South"|country=="SouthYemen"|country=="Yemen People's Republic"|country=="Yemen  (PDR)"|country=="Yemen South"
replace ccode = 690 if country=="Kuwait"
replace ccode = 692 if country=="Bahrain"
replace ccode = 694 if country=="Qatar"
replace ccode = 696 if country=="Unit. Arab Em."
replace ccode = 696 if country=="United Arab Emirates"|country=="UAE"|country=="United Arab Emirates#"
replace ccode = 698 if country=="Oman"
replace ccode = 70 if country=="Mexico"
replace ccode = 700 if country=="Afghanistan"
replace ccode = 701 if country=="Turkmenistan"
replace ccode = 702 if country=="Tajikistan"
replace ccode = 703 if country=="Kyrgyz Republic"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 703 if country=="Kyrgyzstan"
replace ccode = 704 if country=="Uzbekistan"
replace ccode = 705 if country=="Kazakhstan"
replace ccode = 710 if country=="China (PRC)"
replace ccode = 710 if country=="China"
replace ccode = 710 if country=="China, P. R."
replace ccode = 710 if country=="PRC"
replace ccode = 711 if country=="China: Macao SAR"|country=="Macao, China"
replace ccode = 711 if country=="Macao SAR, China"|country=="China, Macao SAR"
replace ccode = 711 if country=="Macao"|country=="Macau"
replace ccode = 712 if country=="Mongolia"
replace ccode = 713 if country=="Taiwan (ROC)"|country=="Taiwan Province of China"|country=="Taiwan, China"|country=="Taiwan, Province of China"
replace ccode = 713 if country=="Taiwan"
replace ccode = 713 if country=="Taiwan"
replace ccode = 731 if country=="Korea (North)"|country=="Korea North"|country=="Korea, Democratic People's Republic of"
replace ccode = 731 if country=="Korea, Dem. Rep."|country=="Democratic People's Republic of Korea"|country=="D.P.R. Korea"
replace ccode = 731 if country=="North Korea"|country=="Korea, North"
replace ccode = 731 if country=="PRK"|country=="North Korea (Democratc People's Republic of Korea)"
replace ccode = 732 if country=="Korea (South)"|country=="Korea South"
replace ccode = 732 if country=="Korea, Rep."|country=="Korea"|country=="Korea (Rep.)"
replace ccode = 732 if country=="Korea, Republic of"
replace ccode = 732 if country=="Korea, South"
replace ccode = 732 if country=="ROK"
replace ccode = 732 if country=="South Korea"|country=="Korea, South"|country=="Republic of Korea"|country=="South Korea (Republic of Korea)"
replace ccode = 740 if country=="Japan"
replace ccode = 750 if country=="India"
replace ccode = 760 if country=="Bhutan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 771 if country=="Bangladesh"
replace ccode = 775 if country=="Burma (Myanmar)"
replace ccode = 775 if country=="Burma"
replace ccode = 775 if country=="Myanmar"|country=="Myanmar (Burma)"
replace ccode = 780 if country=="Sri Lanka"
replace ccode = 781 if country=="Maldives"
replace ccode = 790 if country=="Nepal"
replace ccode = 80 if country=="Belize"
replace ccode = 800 if country=="Thailand"
replace ccode = 811 if country=="Cambodia"
replace ccode = 812 if country=="Lao PDR"|country=="Lao P.D.R."
replace ccode = 812 if country=="Lao People's Democratic Republic"
replace ccode = 812 if country=="Lao"|country=="Lao People's DR"
replace ccode = 812 if country=="Laos"
replace ccode = 812 if country=="Laos"
replace ccode = 817 if country=="Vietnam, South"|country=="South Vietnam"|country=="Vietnam South"
replace ccode = 818 if country=="Viet Nam"|country=="Vietnam, Socialist Republic of"
replace ccode = 818 if country=="Vietnam"|country=="Vietnam North"
replace ccode = 820 if country=="Malaysia"
replace ccode = 830 if country=="Singapore"
replace ccode = 835 if country=="Brunei Darussalam"
replace ccode = 835 if country=="Brunei"
replace ccode = 835 if country=="Brunei"
replace ccode = 840 if country=="Philippines"|country=="Philippines +"
replace ccode = 850 if country=="Indonesia"
replace ccode = 860 if country=="East Timor"|country=="East Timor (Timor L'este)"
replace ccode = 860 if country=="Timor-Leste"|country=="Timor Leste"
replace ccode = 90 if country=="Guatemala"
replace ccode = 900 if country=="Australia"
replace ccode = 91 if country=="Honduras"
replace ccode = 910 if country=="P.N. Guinea"
replace ccode = 910 if country=="Papua New Guinea"|country=="P.N. Guinea"|country=="P. N. Guinea"
replace ccode = 92 if country=="El Salvador"
replace ccode = 920 if country=="New Zealand"
replace ccode = 93 if country=="Nicaragua"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 94 if country=="Costa Rica"
replace ccode = 940 if country=="Solomon Is."
replace ccode = 940 if country=="Solomon Islands"
replace ccode = 941 if country=="Northern Mariana Islands"
replace ccode = 946 if country=="Kiribati"
replace ccode = 947 if country=="Tuvalu"
replace ccode = 95 if country=="Panama"
replace ccode = 950 if country=="Fiji"
replace ccode = 951 if country=="French Polynesia"
replace ccode = 952 if country=="New Caledonia"
replace ccode = 955 if country=="Tonga"
replace ccode = 983 if country=="Marshall Islands"
replace ccode = 986 if country=="Palau"
replace ccode = 987 if country=="Micronesia (Federated States of)"|country=="Micronesia (FS of)"|country=="Micronesia, Federated States of"
replace ccode = 987 if country=="Micronesia"
replace ccode = 987 if country=="Micronesia, Fed. Sts."
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
replace ccode = 991 if country=="Guam"
replace ccode = 992 if country=="Nauru"
replace ccode = 993 if country=="Niue"
replace ccode = 994 if country=="Cook Islands"
replace ccode = 995 if country=="American Samoa"
replace ccode=35 if country=="Anguilla"
replace ccode=36 if country=="Curaçao"
replace ccode=37 if country=="Montserrat"
replace ccode=38 if country=="Sint Maarten (Dutch part)"
replace ccode=39 if country=="British Virgin Islands"
replace ccode=521 if country=="Somaliland"|country=="Somaliland Region"
replace ccode=353 if country=="Northern Cyprus"|country=="Turk Cyprus"
drop if ccode==.
drop if country=="Swaziland"&year>2018
gen presdpi = .
replace presdpi=1 if system==0
replace presdpi=0 if system==1|system==2
label var presdpi "Presidential (DPI); assembly elec. pres and parl = 0"
gen cyear=ccode*10000+year
sort cyear
save "DPI2020work.dta", replace


********************************************************************************************************************
*Ross oil and gas income data. Downloaded from https://dataverse.harvard.edu/api/access/datafile/2431785. 
*added country ccode index and cyear
********************************************************************************************************************
use "rossshort2014.dta", clear
sort cyear
save "rossshort2014.dta", replace

**************************************************************
*Add Przeworski's PIPE data for presidential system variable
**************************************************************
use "pipepres.dta", clear
label var presidential "pres. system=1, parl. system=0; PIPE"
sort cyear
save "pipepres.dta", replace

******************************************************************************
*Hanson and Sigman state capacity data
******************************************************************************
use "HansonSigman_source.dta", clear
replace ccode = 212 if country=="Luxembourg"
replace ccode = 255 if country=="FRG/Germany"|country=="West Germany"|country=="Germany, West"|country=="Germany"|country=="West Germany (FRG)"|country=="German Federal Republic"
replace ccode = 31 if country=="Bahamas"
replace ccode = 316 if country=="Czechoslovakia"
replace ccode = 338 if country=="Malta"
replace ccode = 341 if country=="Montenegro"
replace ccode = 342 if country=="Serbia and Montenegro"|country=="Serbia-Montenegro"
replace ccode = 342 if country=="Yugoslavia"
replace ccode = 343 if country=="F.Y.R. Macedonia"|country=="Macedonia, FYR"|country=="FYR Macedonia"|country=="TFYR of Macedonia"|country=="Macedonia"|country=="The former Yugoslav Republic of Macedonia"|country=="Macedonia, the former Yugoslav Republic of"|country=="TFYR Macedonia"|country=="Macedonia (FYROM)"
replace ccode = 347 if country=="Kosovo"
replace ccode = 365 if country=="Russia"|country=="Russian Federation"|country=="Russia/USSR"|country=="USSR"|country=="Soviet Union"
replace ccode = 395 if country=="Iceland"
replace ccode = 53 if country=="Barbados"
replace ccode = 530 if country=="Ethiopia"
replace ccode = 55 if country=="Grenada"
replace ccode = 56 if country=="St. Lucia"
replace ccode = 591 if country=="Seychelles"
replace ccode = 625 if country=="Sudan"|country=="Sudan (Former)"
replace ccode = 626 if country=="South Sudan"
replace ccode = 770 if country=="Pakistan"
replace ccode = 781 if country=="Maldives"
replace ccode = 80 if country=="Belize"
replace ccode = 835 if country=="Brunei"
replace ccode = 935 if country=="Vanuatu"
replace ccode = 990 if country=="Samoa"|country=="W. Samoa"|country=="Western Samoa"
gen cyear=ccode*10000+year
*duplicates
drop if cyear==2551990&Capacity>2
drop if cyear==3422006&Capacity~=.4128
keep cyear Capacity
sort cyear
save "HansonSigmancap.dta", replace


*Preparing variables for democracy in neighbors (by direct contiguity). Use 2006 contiguity for subsequent years since that is last year in COW contiguity file.
*Be warned that the program sometimes crashes in mid-stream. You may need to increase the numbers after the "sleep" commands to slow it down at key points.

import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
replace state1no=818 if state1no==816&year>1975&year~=.
replace state2no=818 if state2no==816&year>1975&year~=.
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348

sleep 600
save "cont18.dta", replace
sleep 600
keep if year==1816&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "polshort.dta", clear
keep if year==1816
gen cow2 = ccode
keep ccode country polity2 cow2 year
sort cow2
sleep 600
save "polshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "polshortn.dta", uniqusing

sort state1no
by state1no: egen neighpol2 = mean(polity2)
collapse (mean) neighpol2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "polnei.dta",  replace
sleep 600

forvalues i=1817(1)2006 {
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
sleep 600
keep if year==`i'&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600
use "polshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode country polity2 cow2 year
sort cow2
sleep 600
save "polshortn.dta",  replace
sleep 600
use "cont18.dta", clear
sleep 600
gen cow2 = state2no
sort cow2 
merge cow2 using "polshortn.dta", uniqusing

sort state1no
by state1no: egen neighpol2 = mean(polity2)
collapse (mean) neighpol2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "polnei.dta", clear
append using "x.dta"
sleep 600
save "polnei.dta",  replace
sleep 600

}


forvalues i=2007(1)2019 {
import delimited "contdird.txt", clear
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
sleep 600
keep if year==2006&conttype<=3
drop year
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "polshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode country polity2 cow2 year
sort cow2
sleep 600
save "polshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "polshortn.dta", uniqusing

sort state1no
by state1no: egen neighpol2 = mean(polity2)
collapse (mean) neighpol2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "polnei.dta", clear
append using "x.dta"
sleep 600
save "polnei.dta",  replace
sleep 600

}

sort cyear
save "polnei.dta", replace
sleep 600



*VDEM data

import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==1900&conttype<=3

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace

use "vdemshort.dta", clear
keep if year==1900
gen cow2 = ccode
keep ccode vdemdem cow2 year
sort cow2
sleep 600
save "vdemshortn.dta",  replace

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortn.dta", uniqusing

sort state1no
by state1no: egen neighvdem2 = mean(vdemdem)
collapse (mean) neighvdem2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "vdemnei.dta",  replace

forvalues i=1901(1)2006 {
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
keep if year==`i'&conttype<=3

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "vdemshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode vdemdem cow2 year
sort cow2
sleep 600
save "vdemshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortn.dta", uniqusing

sort state1no
by state1no: egen neighvdem2 = mean(vdemdem)
collapse (mean) neighvdem2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "vdemnei.dta", clear
append using "x.dta"
sleep 600
save "vdemnei.dta",  replace
}


forvalues i=2007(1)2021 {
import delimited "contdird.txt", clear
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
sleep 600
keep if year==2006&conttype<=3
drop year

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "vdemshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode vdemdem cow2 year
sort cow2
sleep 600
save "vdemshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortn.dta", uniqusing

sort state1no
by state1no: egen neighvdem2 = mean(vdemdem)
collapse (mean) neighvdem2 year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "vdemnei.dta", clear
append using "x.dta"
sleep 600
save "vdemnei.dta",  replace
}

sort cyear
save "vdemnei.dta", replace

*VDEM lib dem data
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==1900&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace

use "vdemshort.dta", clear
keep if year==1900
gen cow2 = ccode
keep ccode vdemlib cow2 year
sort cow2
sleep 600
save "vdemshortnl.dta",  replace

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortnl.dta", uniqusing

sort state1no
by state1no: egen neighvdem2l = mean(vdemlib)
collapse (mean) neighvdem2l year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "vdemneil.dta",  replace

forvalues i=1901(1)2006 {
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==`i'&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "vdemshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode vdemlib cow2 year
sort cow2
sleep 600
save "vdemshortnl.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortnl.dta", uniqusing

sort state1no
by state1no: egen neighvdem2l = mean(vdemlib)
collapse (mean) neighvdem2l year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600

use "vdemneil.dta", clear
append using "x.dta"
sleep 600
save "vdemneil.dta",  replace
}


forvalues i=2007(1)2021 {
import delimited "contdird.txt", clear
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
keep if year==2006&conttype<=3
drop year
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "vdemshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode vdemlib cow2 year
sort cow2
sleep 600
save "vdemshortnl.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "vdemshortnl.dta", uniqusing

sort state1no
by state1no: egen neighvdem2l = mean(vdemlib)
collapse (mean) neighvdem2l year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "vdemneil.dta", clear
append using "x.dta"
sleep 600
save "vdemneil.dta",  replace
}

sort cyear
save "vdemneil.dta", replace



*FH Data

import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==1972&conttype<=3

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace

use "fh20polshort.dta", clear
keep if year==1972
gen cow2 = ccode
keep ccode fhpol cow2 year
sort cow2
sleep 600
save "fhpolshortn.dta",  replace

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "fhpolshortn.dta", uniqusing

sort state1no
by state1no: egen neighfhpol = mean(fhpol)
collapse (mean) neighfhpol year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "fhfnei.dta",  replace

forvalues i=1973(1)2006 {
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==`i'&conttype<=3

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "fh20polshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode fhpol cow2 year
sort cow2
sleep 600
save "fhpolshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "fhpolshortn.dta", uniqusing

sort state1no
by state1no: egen neighfhpol = mean(fhpol)
collapse (mean) neighfhpol year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "fhfnei.dta", clear
append using "x.dta"
sleep 600
save "fhfnei.dta",  replace
}


forvalues i=2007(1)2021 {
import delimited "contdird.txt", clear
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 600
save "cont18.dta", replace
keep if year==2006&conttype<=3
drop year

gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "fh20polshort.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode fhpol cow2 year
sort cow2
sleep 600
save "fhpolshortn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "fhpolshortn.dta", uniqusing

sort state1no
by state1no: egen neighfhpol = mean(fhpol)
collapse (mean) neighfhpol year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 600
save "x.dta",  replace
sleep 600
use "fhfnei.dta", clear
append using "x.dta"
sleep 600
save "fhfnei.dta",  replace
}

sort cyear
save "fhfnei.dta", replace

*LIED
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 200
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
save "cont18.dta", replace
keep if year==1816&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 200
save "cont18.dta", replace

use "lied_v3wk.dta", clear
keep if year==1816
gen cow2 = ccode
keep ccode country lied6 cow2 year
sort cow2
sleep 200
save "liedn.dta",  replace

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "liedn.dta", uniqusing

sort state1no
by state1no: egen neighlied = mean(lied6)
collapse (mean) neighlied year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 200
save "liednei.dta",  replace

forvalues i=1817(1)2006 {
import delimited "contdird.txt", clear
replace state1no=255 if state1no==260&year<1990
replace state2no=255 if state2no==260&year<1990
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 200
replace state1no=316 if state1no==315
replace state2no=316 if state2no==315
sleep 600
save "cont18.dta", replace
sleep 600
keep if year==`i'&conttype<=3
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "lied_v3wk.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode country lied6 cow2 year
sort cow2
sleep 200
save "liedn.dta",  replace
sleep 600

use "cont18.dta", clear
sleep 200
gen cow2 = state2no
sort cow2 
merge cow2 using "liedn.dta", uniqusing

sort state1no
by state1no: egen neighlied = mean(lied6)
collapse (mean) neighlied year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 200
save "x.dta",  replace
sleep 600
use "liednei.dta", clear
append using "x.dta"
sleep 200
save "liednei.dta",  replace
}


forvalues i=2007(1)2021 {
import delimited "contdird.txt", clear
replace state1no=341 if state1no==348
replace state2no=341 if state2no==348
sleep 200
save "cont18.dta", replace
sleep 200
keep if year==2006&conttype<=3
drop year
gen dyadid =  state1no*10000+state2no
sleep 600
save "cont18.dta", replace
sleep 600

use "lied_v3wk.dta", clear
keep if year==`i'
gen cow2 = ccode
keep ccode country lied6 cow2 year
sort cow2
sleep 600
save "liedn.dta",  replace
sleep 600

use "cont18.dta", clear
gen cow2 = state2no
sort cow2 
merge cow2 using "liedn.dta", uniqusing

sort state1no
by state1no: egen neighlied = mean(lied6)
collapse (mean) neighlied year, by(state1no)
replace state1no=342 if state1no==345
drop if state1no==.
gen cyear = state1no*10000+year
sort cyear
sleep 200
save "x.dta",  replace
sleep 600
use "liednei.dta", clear
sleep 200
append using "x.dta"
sleep 200
save "liednei.dta",  replace
}

sort cyear
save "liednei.dta", replace
